package com.yohann.algorithm;

/**
 * <p>
 *
 * </p>
 *
 * @author Yohann
 * @since 2020/9/22
 */
public class DoubleLinkedListTest {
    public static void main(String[] args) {
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.add(new HeroNode2(4,"宋江","及时雨"));
        doubleLinkedList.add(new HeroNode2(2,"卢俊义","玉麒麟"));
        doubleLinkedList.add(new HeroNode2(1,"吴用","智多星"));
        doubleLinkedList.add(new HeroNode2(1,"林冲","豹子头"));

        doubleLinkedList.display();
        System.out.println("*******************");
        doubleLinkedList.update(new HeroNode2(4,"林冲","豹子头"));
        doubleLinkedList.display();

        System.out.println("*******************");
        doubleLinkedList.delete(1);
        doubleLinkedList.display();
    }
}

class DoubleLinkedList{
    private HeroNode2 head = new HeroNode2(0,"","");

    public HeroNode2 getHead() {
        return head;
    }

    public void display(){
        if (head.next == null){
            System.out.println("链表为空！");
            return;
        }

        HeroNode2 temp = head.next;
        while (true){
            System.out.println(temp);

            if (temp.next == null){
                break;
            }
            temp = temp.next;
        }
    }

    public void add(HeroNode2 heroNode){
        HeroNode2 temp = head;

        while (true){
            //头结点没有下一个指向
            if (temp.next == null){
                break;
            }

            //一直指向空节点才结束循环，即到达链表尾部
            temp = temp.next;
        }

        //在尾部加入节点
        temp.next = heroNode;
        //头部指向上一个节点
        heroNode.pre = temp;
    }

    //修改
    public void update(HeroNode2 heroNode){
        HeroNode2 temp = head;
        boolean flag = false;

        while (true){
            temp = temp.next;

            if (temp == null){
                break;
            }

            if (temp.no == heroNode.no){
                flag = true;
                break;
            }
        }

        if (flag){
            temp.name = heroNode.name;
            temp.nickname = heroNode.nickname;
        }else {
            System.out.println("不存在"+heroNode.no+"节点");
        }
    }

    public void delete(int num){
        HeroNode2 temp = head.next;
        boolean flag = false;

        while (true){
            if (temp == null){
                break;
            }
            if (temp.no == num) {
                flag = true;
                break;
            }

            temp = temp.next;
        }

        if (flag){
            //上一个节点的下一个指向为需删除节点的下一个指向
            temp.pre.next = temp.next;

            //删除的节点不是最后一个，需要修改后面节点的指向
            if (temp.next != null){
                //下一个节点的上一个指向为需删除节点的上一个指向
                temp.next.pre = temp.pre;
            }
        }else {
            System.out.println("不存在"+num+"节点");
        }
    }
}

class HeroNode2{
    int no;
    String name;
    String nickname;
    HeroNode2 pre;
    HeroNode2 next;

    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}